package com.okilo.online.app.tools.decoration

import android.content.Context
import android.graphics.Rect
import android.util.TypedValue
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration

/**
 * GridSpacingItemDecoration
 * @Author wwdeng
 * @CreateTime: 2024/10/17 14:46
 * @Annotation：GridLayoutManager（网格布局）设置item的间隔
 */
class GridSpacingItemDecoration private constructor(
    private val spanCount: Int,
    private val spacing: Int,
    private val includeEdge: Boolean,
    private val includeTop: Boolean,
    private val includeBottom: Boolean,
    private val topSpacingType: TopSpacingType = TopSpacingType.FIRST_ROW_ONLY
) : ItemDecoration() {

    enum class TopSpacingType {
        FIRST_ROW_ONLY,    // 只在第一行添加顶部间距
        ALL_ROWS,          // 所有行都添加顶部间距
        BETWEEN_ROWS_ONLY  // 只在行与行之间添加间距（第一行无顶部间距）
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val position = parent.getChildAdapterPosition(view)
        if (position == RecyclerView.NO_POSITION) return

        val column = position % spanCount
        val row = position / spanCount

        if (includeEdge) {
            outRect.left = spacing - column * spacing / spanCount
            outRect.right = (column + 1) * spacing / spanCount

            // 顶部间距处理
            if (includeTop) {
                when (topSpacingType) {
                    TopSpacingType.FIRST_ROW_ONLY -> {
                        if (row == 0) outRect.top = spacing
                    }
                    TopSpacingType.ALL_ROWS -> {
                        outRect.top = spacing
                    }
                    TopSpacingType.BETWEEN_ROWS_ONLY -> {
                        // 第一行无顶部间距
                    }
                }
            }

            // 底部间距
            if (includeBottom) {
                outRect.bottom = spacing
            }
        } else {
            outRect.left = column * spacing / spanCount
            outRect.right = spacing - (column + 1) * spacing / spanCount

            // 顶部间距处理
            if (includeTop) {
                when (topSpacingType) {
                    TopSpacingType.FIRST_ROW_ONLY -> {
                        if (row == 0) outRect.top = spacing
                    }
                    TopSpacingType.ALL_ROWS -> {
                        outRect.top = spacing
                    }
                    TopSpacingType.BETWEEN_ROWS_ONLY -> {
                        // 第一行无顶部间距
                    }
                }
            }
        }

        // 处理行间间距（适用于 BETWEEN_ROWS_ONLY 模式）
        if (includeTop && topSpacingType == TopSpacingType.BETWEEN_ROWS_ONLY && row > 0) {
            outRect.top = spacing
        }
    }

    companion object {
        @JvmOverloads
        fun create(
            spanCount: Int,
            spacingDp: Int,
            context: Context,
            includeEdge: Boolean = true,
            includeTop: Boolean = true,
            includeBottom: Boolean = true,
            topSpacingType: TopSpacingType = TopSpacingType.FIRST_ROW_ONLY
        ): GridSpacingItemDecoration {
            val spacingPx = dpToPx(spacingDp, context)
            return GridSpacingItemDecoration(
                spanCount,
                spacingPx,
                includeEdge,
                includeTop,
                includeBottom,
                topSpacingType
            )
        }

        private fun dpToPx(dp: Int, context: Context): Int {
            return TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                dp.toFloat(),
                context.resources.displayMetrics
            ).toInt()
        }
    }
}
